package algorithm.easy;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution144 {

    //输入：root = [1,null,2,3]
    //输出：[1,2,3]
    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        if(node==null){
            return null;
        }
        while(node!=null || !stack.isEmpty()){
            if(node!=null){
                stack.push(node);
                list.add(node.val);
                node = node.left;
            }else{
                node = stack.pop();
                node = node.right;
            }
        }


        return list;
    }

    public static void main(String[] args){
        TreeNode treeNode = TreeNode.init110();
        System.out.println(preorderTraversal(treeNode));
    }
}
